package zarelli.biff;

import android.app.Activity;
import android.app.ProgressDialog;
import android.media.MediaPlayer;
import android.util.Log;
import android.view.View;
import android.webkit.WebChromeClient;
import android.widget.FrameLayout;
import android.widget.VideoView;

/**
 *
 * @author guilherme
 */
public class MyChromeClient extends WebChromeClient implements MediaPlayer.OnCompletionListener, MediaPlayer.OnPreparedListener {

    private static final String TAG = "WebViewVideo.MyChromeClient";
    private ProgressDialog progressDialog;
    private Activity activity;
    private CustomViewCallback callback;
    private VideoView video;
    private FrameLayout viewContainer;
    private FrameLayout.LayoutParams layoutParameters;
    private int idLinearLayoutView;

    public MyChromeClient(Activity activity, int idLinearLayoutView) {
        this.activity = activity;
        layoutParameters = new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.FILL_PARENT,
                FrameLayout.LayoutParams.FILL_PARENT);
        this.idLinearLayoutView = idLinearLayoutView;
    }

    @Override
    public void onShowCustomView(View view, CustomViewCallback callback) {
        super.onShowCustomView(view, callback);
        this.callback = callback;
        if (view instanceof FrameLayout) {
            this.viewContainer = (FrameLayout) view;
            if (this.viewContainer.getFocusedChild() instanceof VideoView) {
                progressDialog = new ProgressDialog(activity);
                progressDialog.setMessage("Aguarde...");
                progressDialog.setCancelable(false);
                progressDialog.show();

                this.video = (VideoView) this.viewContainer.getFocusedChild();
                activity.findViewById(idLinearLayoutView).setVisibility(View.GONE);
                this.viewContainer.setVisibility(View.VISIBLE);

                this.activity.addContentView(this.viewContainer,
                        this.layoutParameters);
                this.video.setOnCompletionListener(this);
                this.video.setOnPreparedListener(this);
                this.video.start();
            }
        }
    }

    @Override
    public void onHideCustomView() {
        try {
            stopVideo();
        } catch (Exception e) {
            Log.e(TAG, ".onHideCustomView", e);
        }
    }

    public boolean stopVideo() throws Exception {
        if (video != null && (video.isPlaying() || video.isFocusable())) {
            video.stopPlayback();
            if (callback != null) {
                callback.onCustomViewHidden();
                
            }
            activity.findViewById(idLinearLayoutView).setVisibility(View.VISIBLE);
        } else {
            return false;
        }

        return true;
    }

    public void onCompletion(MediaPlayer arg0) {
        try {
            stopVideo();
        } catch (Exception e) {
            Log.e(TAG, ".onCompletion", e);
        }

    }

    public void onPrepared(MediaPlayer arg0) {
        try {
            progressDialog.dismiss();
        } catch (Exception e) {
            Log.e(TAG, ".onPrepared", e);
        }
    }
}
